
//
// 110719 - AH - (Garage) Created.
// 110804 - AH - (Bybanen *) Moved from package 
//               org.soekrat.gui.swing.chat.objectconversation
// 110805 - AH - Renamed Cancel button Delete because that is it's true effect.
//

package org.soekrat.gui.swing.chat.objectconversation.note;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 * <p>
 *   Dialog for making notes.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC) 
 */
@SuppressWarnings("serial")
public final class NoteDialog extends JDialog 
{
  private final JTextArea m_textArea=new JTextArea(20, 40);
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public NoteDialog(){ buildGui(); }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param p Dailog's parent.
   */
  public NoteDialog(Frame p){ super(p); buildGui(); }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param p Dailog's parent.
   */
  public NoteDialog(Dialog p){ super(p); buildGui(); }
  
  // Invoked from constructors.
  private void buildGui()
  {
    setModal(true);
    setTitle("Note");
    
    Container c=getContentPane();
    
    c.setLayout(new BorderLayout());
    
    c.add(new JScrollPane(m_textArea));
    
    JPanel panel=new JPanel(new FlowLayout(FlowLayout.CENTER));
    panel.add
    (
      new JButton
      (
        new AbstractAction("OK")
        {
          public void actionPerformed(ActionEvent e){ dispose(); }
        }
      )
    );
    panel.add
    (
      new JButton
      (
        new AbstractAction("Delete")
        {
          public void actionPerformed(ActionEvent e)
          {
            m_textArea.setText("");
            dispose();
          }
        }
      )
    );
    
    c.add(panel, BorderLayout.SOUTH);
    
    pack();
  }
  
  private String m_oldText=null;
  
  /**
   * <p>
   *   Shows dialog.
   * </p>
   * @param txt Text to show, may be {@code null} if no text is to be displayed.
   * @return Note text or {@code null} if no note was made.
   */
  public String showDialog(String txt)
  {
    txt=(txt==null) ? "" : txt.trim();
    
    m_oldText=(txt.length()==0) ? null : txt;
    
    m_textArea.setText(txt);
    setVisible(true);
    String text=m_textArea.getText();
    text=(text==null) ? "" : text.trim();
    
    if (text.length()==0) return null;
    
    return text;
  }
  
}
